<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\appv110\model\StaffNote;
use app\appv110\model\StaffNoteBack;
use app\appv110\model\StaffBindStore;
use app\appv110\model\Staff;

class StoreController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 100000  没有发现该备注
         * 100001  
         * 100002  
         * 100003  
         * 100004  
         * 100005  
         * 100006  
         * 
         */
    }

    /*
     * 给用户添加备注
     * author xfwed
     * date 2021.06.17
     */

    public function addStoreNote() {
        $rule = [
            'store_id' => 'require',
            'ctm_id' => 'require',
//            'note' => 'require',
//            'name' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'ctm_id.require' => '用户id必须',
//            'note.require' => '备注必须',
//            'name.require' => '备注名字必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $store_id = $all['store_id']; //店铺id
        $ctm_id = $all['ctm_id']; //用户id
        $note = input('param.note'); //备注
        $name = input('param.name'); //备注姓名
        $row = Db::name('store_note')
                ->where('store_id', $store_id)
                ->where('ctm_id', $ctm_id)
                ->find();
        Db::startTrans();
        $data['store_id'] = $store_id;
        $data['ctm_id'] = $ctm_id;
        $data['note'] = $note;
        $data['name'] = $name;
        $data['add_time'] = date('Y-m-d H:i:s', time());
        if ($row) {
            if ($name) {
                $save = Db::name('store_note')
                        ->where('store_id', $store_id)
                        ->where('ctm_id', $ctm_id)
                        ->update(['name' => $name, 'note' => $note, 'add_time' => date('Y-m-d H:i:s', time())]);
            } else {
                $save = TRUE;
            }
        } else {
            $save = TRUE;
            $insert = Db::name('store_note')->insert($data);
        }
        $insert = Db::name('store_note_back')->insert($data);
        if ($save && $insert) {
            Db::commit();
            return Utils::ajaxReturn(0, '备注成功', null);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(40000, '备注失败', null);
        }
    }

    /*
     * 修改备注
     * author xfwed
     * date 2021.06.17
     */

    public function saveStoreNote() {
        $rule = [
            'id' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $note = input('param.note'); //备注
        $name = input('param.name'); //备注姓名
        $row = Db::name('store_note_back')->where('id', $id)->find();
        Db::startTrans();
        if ($name) {
            $save = Db::name('store_note')
                    ->where('store_id', $row['store_id'])
                    ->where('ctm_id', $row['ctm_id'])
                    ->update(['name' => $name]);
        }
        $saveRow = Db::name('store_note_back')->where('id', $id)->update(['name' => $name, 'note' => $note]);

        if ($saveRow) {
            Db::commit();
            return Utils::ajaxReturn(0, '修改备注成功', null);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(40002, '修改备注失败', null);
        }
    }

    /*
     * 删除备注
     * author xfwed
     * date 2021.06.17
     */

    public function delStoreNote() {
        $rule = [
            'id' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $row = Db::name('store_note_back')->where('id', $id)->find();
        $noteCount = Db::name('store_note_back')
                ->where('store_id', $row['store_id'])
                ->where('ctm_id', $row['ctm_id'])
                ->count();
        if (empty($row)) {
            return Utils::ajaxReturn(40001, '没有发现该备注', null);
        } else {
            if ($noteCount == 1) {
                Db::name('store_note')->where('store_id', $row['store_id'])
                        ->where('ctm_id', $row['ctm_id'])
                        ->delete();
            }
            $saveRow = Db::name('store_note_back')->where('id', $id)->delete();
        }

        return Utils::ajaxReturn(0, '删除备注成功', null);
    }

    /*
     * 备注记录
     * author xfwed
     * date 2021.06.17
     */

    public function storeNoteList() {
        $rule = [
            'store_id' => 'require',
            'ctm_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'type.require' => '类型必须',
            'store_id.require' => '店铺id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $store_id = $all['store_id'];
        $type = $all['type']; //1自己备注 2美容师备注
        $perPage = isset($all['perPage']) ? $all['perPage'] : 10;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        if ($type == 1) {
            $list = Db::name('store_note_back')
                    ->where('store_id', $store_id)
                    ->where('ctm_id', $ctm_id)
                    ->limit($offset, $perPage)
                    ->order('id desc')
                    ->field('id,note,name,add_time')
                    ->select();
        } elseif ($type == 2) {
            $list = StaffNoteBack::where('ctm_id', $ctm_id)
                    ->limit($offset, $perPage)
                    ->order('id desc')
                    ->field('id,note,name,add_time')
                    ->select();
        }
        return Utils::ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 备注详情
     * author xfwed
     * date 2021.06.17
     */

    public function storeNoteInfo() {
        $rule = [
            'id' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return Utils::ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $row = Db::name('store_note_back')->where('id', $id)->find();
        if (empty($row)) {
            return Utils::ajaxReturn(100000, '没有发现该备注', null);
        } else {
            return Utils::ajaxReturn(0, '查询成功', $row);
        }
    }

}
